package com.omp.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.omp.common.dao.CommonDAO;
import com.omp.dao.ProductCatalogDAO;
import com.omp.dao.ProductDAO;
import com.omp.entity.Product;
import com.omp.entity.ProductCatalog;
import com.omp.service.ProductService;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {
	
	@Autowired
	private CommonDAO commonDAO;
	@Autowired
	private ProductDAO productDAO;
	@Autowired
	private ProductCatalogDAO productCatalogDAO;

	@Override
	public void save(Product product) {
		commonDAO.save(product);
	}

	@Override
	public void update(Product product) {
		commonDAO.update(product);
	}

	@Override
	public void delete(Integer id) {
		productDAO.deleteById(id);
	}

	@Override
	public void batchDelete(List<Integer> ids) {
		if (ids != null && ids.size() > 0) {
			for (Integer id : ids) {
				productDAO.deleteById(id);
			}
		}
	}

	@Override
	public Product findById(Integer id) {
		return (Product) commonDAO.findById(Product.class, id);
	}

	@Override
	public Page<Product> findByPage(Page<Product> page, Product product) {
		return productDAO.findByPage(page, product);
	}

	@Override
	public boolean nameExists(String name) {
		return productDAO.nameExists(name);
	}

	@Override
	public void saveProductCatalog(Integer industryId, List<Integer> productIds) {
//		List<ProductCatalog> productCatalogList = productCatalogDAO.findByIndustryId(industryId);
//		Map<String, ProductCatalog> productCatalogMap = new HashMap<String, ProductCatalog>();
//		if (productCatalogList != null && productCatalogList.size() > 0) {
//			for (ProductCatalog catalog : productCatalogList) {
//				productCatalogMap.put(catalog.getIndustry().getId() + "&" + catalog.getProduct().getId(), catalog);
//			}
//		}
		if (industryId != null) {
			productCatalogDAO.deleteByIndustryId(industryId);
		}
		if (productIds != null && productIds.size() > 0) {
			for (Integer id : productIds) {
				ProductCatalog productCatalog = new ProductCatalog();
				productCatalog.setProductId(id);
				productCatalog.setIndustryId(industryId);
				ProductCatalog catalog = productCatalogDAO.findById(industryId, id);
				if (catalog != null) {
					productCatalog.setBeginDate(catalog.getBeginDate());
					productCatalog.setEndDate(catalog.getEndDate());
				}
				commonDAO.save(productCatalog);
			}
		}
	}

	@Override
	public List<Product> findByIndustryId(Integer industryId, Product product) {
		return productDAO.findByIndustryId(industryId, product);
	}

	@Override
	public List<Product> findByIndustryId(Integer industryId, Product product, boolean findNoneIndustry) {
		return productDAO.findByIndustryId(industryId, product, findNoneIndustry);
	}

	@Override
	public void saveValidDate(Integer industryId, List<Integer> productIds, String beginDate, String endDate) {
		if (productIds != null && productIds.size() > 0) {
			for (Integer id : productIds) {
				ProductCatalog catalog = productCatalogDAO.findById(industryId, id);
				if (catalog != null) {
					catalog.setBeginDate(beginDate);
					catalog.setEndDate(endDate);
					commonDAO.update(catalog);
				} else {
					ProductCatalog productCatalog = new ProductCatalog();
					productCatalog.setProductId(id);
					productCatalog.setIndustryId(industryId);
					productCatalog.setBeginDate(beginDate);
					productCatalog.setEndDate(endDate);
					commonDAO.save(productCatalog);
				}
			}
		}
	}

}
